package com.may.resttemplate.utils;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.Map;

/**
 EXCHANGE请求参数说明：
 *
 *     第一个参数是url。
 *     第二个参数是请求方式。
 *     第三个参数是请求实体。
 *     第四个参数是返回值类型。
 *     第五个参数是uri地址变量。
 *
 * @program: template
 * @description: 封装方法
 * @author: Bobby.Ma
 * @create: 2020-06-12 17:06
 **/
@Component
public class RequestUtil{

    @Autowired
    private RestTemplate restTemplate;

    private String base;

    public void setBase(String base){
        this.base = base;
    }

    public String getBase(){
        return this.base;
    }

    /**
     * 无参及参数在 url 中的 get 请求方式
     * @param mapping 请求 mapping
     * @param responseType
     * @param uriVariables
     * @param <T>
     * @return
     */
    public <T> ResponseEntity<T> get(String mapping, Class<T> responseType, Object... uriVariables){
        HttpEntity<?> httpEntity = HttpEntityUtil.setEntity();
        return restTemplate.exchange(base + mapping, HttpMethod.GET,httpEntity,responseType,uriVariables);
    }
    public ResponseEntity<JSONObject> get(String mapping, Object... uriVariables){
        HttpEntity<?> httpEntity = HttpEntityUtil.setEntity();
        return restTemplate.exchange(base + mapping, HttpMethod.GET,httpEntity, JSONObject.class,uriVariables);
    }

    /**
     * header 中及 url 中携带参数的 get 请求方式
     * @param mapping 请求 mapping
     * @param httpEntity
     * @param responseType
     * @param uriVariables
     * @param <T>
     * @return
     */
    public <T> ResponseEntity<T> get(String mapping, HttpEntity<?> httpEntity, Class<T> responseType, Object... uriVariables){
        return restTemplate.exchange(base + mapping, HttpMethod.GET,httpEntity,responseType,uriVariables);
    }
    public ResponseEntity<JSONObject> get(String mapping, HttpEntity<?> httpEntity, Object... uriVariables){
        return restTemplate.exchange(base + mapping, HttpMethod.GET,httpEntity,JSONObject.class,uriVariables);
    }
    public ResponseEntity<JSONObject> get(String mapping, HttpEntity<?> httpEntity, Map<String, ?> uriParam,Object... uriVariables){
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(base + mapping);
        uriParam.forEach(builder::queryParam);
        return restTemplate.exchange(builder.build().toString(), HttpMethod.GET,httpEntity,JSONObject.class,uriVariables);
    }
    public ResponseEntity<JSONObject> get(String mapping, HttpEntity<?> httpEntity, Map<String, ?> uriParam){
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(base + mapping);
        uriParam.forEach(builder::queryParam);
        return restTemplate.exchange(builder.build().encode().toString(), HttpMethod.GET,httpEntity,JSONObject.class);
    }

    /**
     * header 中及 body 中及 url 中携带参数的 post 请求方式
     * @param mapping 请求 mapping
     * @param httpEntity
     * @param responseType
     * @param uriVariables
     * @param <T>
     * @return
     */
    public <T> ResponseEntity<T> post(String mapping, HttpEntity<?> httpEntity, Class<T> responseType, Object... uriVariables){
        return restTemplate.exchange(base + mapping, HttpMethod.POST,httpEntity,responseType,uriVariables);
    }
    public ResponseEntity<JSONObject> post(String mapping, HttpEntity<?> httpEntity, Object... uriVariables){
        return restTemplate.exchange(base + mapping, HttpMethod.POST,httpEntity,JSONObject.class,uriVariables);
    }

    /**
     * header 中及 body 中及 url 中携带参数的 put 请求方式
     * @param mapping 请求 mapping
     * @param httpEntity
     * @param responseType
     * @param uriVariables
     * @param <T>
     * @return
     */
    public <T> ResponseEntity<T> put(String mapping, HttpEntity<?> httpEntity, Class<T> responseType, Object... uriVariables){
        return restTemplate.exchange(base + mapping, HttpMethod.PUT,httpEntity,responseType,uriVariables);
    }
    public ResponseEntity<JSONObject> put(String mapping, HttpEntity<?> httpEntity, Object... uriVariables){
        return restTemplate.exchange(base + mapping, HttpMethod.PUT,httpEntity,JSONObject.class,uriVariables);
    }

    /**
     * header 中及 body 中及 url 中携带参数的 delete 请求方式
     * @param mapping 请求 mapping
     * @param httpEntity
     * @param responseType
     * @param uriVariables
     * @param <T>
     * @return
     */
    public <T> ResponseEntity<T> delete(String mapping, HttpEntity<?> httpEntity, Class<T> responseType, Object... uriVariables){
        return restTemplate.exchange(base + mapping, HttpMethod.DELETE,httpEntity,responseType,uriVariables);
    }
    public ResponseEntity<JSONObject> delete(String mapping, HttpEntity<?> httpEntity, Object... uriVariables){
        return restTemplate.exchange(base + mapping, HttpMethod.DELETE,httpEntity,JSONObject.class,uriVariables);
    }

    /**
     * 无参及参数在 url 中的 delete 请求方式
     * @param mapping
     * @param responseType
     * @param uriVariables
     * @param <T>
     * @return
     */
    public <T> ResponseEntity<T> delete(String mapping, Class<T> responseType, Object... uriVariables){
        HttpEntity<?> httpEntity = HttpEntityUtil.setEntity();
        return restTemplate.exchange(base + mapping, HttpMethod.DELETE,httpEntity,responseType,uriVariables);
    }
    public ResponseEntity<JSONObject> delete(String mapping, Object... uriVariables){
        HttpEntity<?> httpEntity = HttpEntityUtil.setEntity();
        return restTemplate.exchange(base + mapping, HttpMethod.DELETE,httpEntity,JSONObject.class,uriVariables);
    }
}
